<template>
  <div class="loginbody" v-wave="{
    color: '#fff',
    easing: 'ease-out',
    duration: 0.6,
    dissolveDuration: 0.25,
    initialOpacity: 0,

    finalOpacity: 0.1,
    cancellationPeriod: 75,
    trigger: 'auto',
    tagName: 'div'
  }">
    <div class="login-import-item link-type" @click.prevent="toHome">返回首页</div>
    <div class="loginmain" :class="gotu">
      <div class="login-table">
        <img src="../assets/images/denglu_logo.png" width="40px" height="40px" alt="">
        <p class="title-log">能力开放平台</p>
      </div>
      <div class="logbox">
        <div class="formbox">
          <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">

            <h3 class="title" style="font-size:18px;font-weight:500;color:white;">用户登录</h3>
            <el-form-item prop="username">
              <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="请输入用户名">
                <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
              </el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="请输入密码"
                @keyup.enter.native="handleLogin">
                <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
              </el-input>

            </el-form-item>
            <el-form-item prop="code" v-if="captchaEnabled">
              <!-- <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%"
                @keyup.enter.native="handleLogin">
                <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
              </el-input>
              <div class="login-code">
                <img :src="codeUrl" @click="getCode" class="login-code-img" />
              </div> -->
              <div style="float:right;transform:translateY(-20px)">
                <p class="link-type" @click.prevent="retrieve">忘记密码？</p>
              </div>
            </el-form-item>

            <el-checkbox v-model="loginForm.rememberMe"
              style="margin:0px 0px 25px 0px">记住密码</el-checkbox>
            <el-form-item style="width:100%;transform:translateY(30px)">
              <el-button :loading="loading" size="medium" type="primary" style="width:100%;"
                @click.native.prevent="handleLogin">
                <span v-if="!loading" v-wave>登 录</span>
                <span v-else>登 录 中...</span>
              </el-button>
              <div class="login-register" v-if="register">
                <span>还没有账号？</span><span style="color: rgb(30, 204, 152)" class="link-type" @click="gozc">立即注册</span>
              </div>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
    <div :class="an">
      <div class="anbox1"></div>
      <div class="anbox2"></div>
      <div class="anbox3"></div>
      <div class="anbox4"></div>
      <div class="anbox5"></div>
      <div class="anbox6"></div>
      <div class="anbox7"></div>
      <div class="anbox8"></div>
    </div>

  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "admin",
        password: "admin123",
        rememberMe: false,
        code: "",
        an: "",
        uuid: ""
      },
      gotu: '',
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        // code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: true,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    retrieve() {
      this.$router.push("/retrieve");
    },
    gozc() {
      this.gotu = 'golog'
      setTimeout(() => {
        this.$router.push("/register");
      }, 700)
    },

    toHome() {
      this.$router.push("/home");
    },
    // gologin() {
    //   this.$router.push("/retrieve")
    // },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }

          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.an = "animation-bg"
            setTimeout(() => {
              this.$router.push({ path: this.redirect || "/" }).catch(() => { });

            }, 700);
            this.loading = false;

          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    }
  },
  beforeRouteEnter(to, from, next) {
    console.log(from.fullPath, 'beforeRouteEnter');

    next(vm => {
      if (from.fullPath === '/register') {
        vm.gotu = 'gologre '
      }
      // vm相当于上面的this
      vm.fromPage = from.path;
    });

  },
};
</script>

<style rel="stylesheet/scss" lang="scss">
.loginbody {
  width: 100%;
  height: 100%;
  background: linear-gradient(rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0.7)), url(../assets/images/g2.jpg) no-repeat 0% 20%/ cover;
  // background: linear-gradient(100deg, rgb(71, 186, 170), rgb(38, 178, 243), rgb(105, 198, 237));
  position: relative;

  // min-width: 1500px;
  overflow: hidden;
  @keyframes logtore {
    to {
      transform: rotate(0deg);
      opacity: 1;
    }

  }

  .gologre {
    transform: rotate(-90deg);
    animation: logtore 0.7s linear forwards;
    transform-origin: 150px 1200px;
    opacity: 0;

  }


  @keyframes logto {
    to {
      transform: rotate(-90deg);
      opacity: 0;
    }

  }

  .golog {
    transform-origin: 150px 1200px;
    animation: logto 0.7s linear forwards;
  }

  .loginmain {
    width: 405px;
    height: 500px;
    margin: 150px auto;
    position: relative;
    border-radius: 6px;
  }

  .login-import-item {
    position: absolute;
    top: 2%;
    left: 2%;
    color: white;
    font-size: 16px;
    z-index: 33;
  }

  .logbox {
    position: relative;
    width: 412px;
    height: 525px;
    overflow: hidden;
    border-radius: 6px;

    .formbox {
      position: absolute;
      inset: 3px;
      background: rgb(34, 34, 36);
      z-index: 10;
      border-radius: 6px;
      border: 3px solid #000000;

      .login-form {
        border-radius: 6px;
        background: rgb(34, 34, 36);
        width: 400px;
        padding: 25px 30px 5px 30px;
        height: 430px;

        .el-input {
          height: 38px;
          background: rgb(34, 34, 36);
          margin-bottom: 15px;
          border: 2px solid rgb(34, 34, 36);

          input:focus {
            border: 2px solid rgb(30, 204, 152);
            transition: 0.6s;
            border-radius: 10px;
            box-sizing: border-box;
          }

          input {
            height: 52px;
            background: rgb(34, 34, 36);
            border: 2px solid rgb(34, 34, 36);
            // border-radius: 10px;
            color: white;
            border-bottom: 2px solid rgb(30, 204, 152);
            transition: 0.6s;
            box-sizing: border-box;
          }
        }

        // .el-checkbox__inner {
        //   background: transparent;
        //   border: 1px solid #00ffe5;
        // }
        .el-checkbox__inner {
          background: transparent;
          border: 1px solid rgb(30, 204, 152);
          ;
        }

        .el-checkbox__label {
          color: rgb(30, 204, 152);
        }

        .el-button {
          background: rgb(30, 204, 152);
          border: 1px solid rgb(30, 204, 152);
          font-size: 18px;
        }

        .link-type {
          color: rgb(30, 204, 152);

        }

        .input-icon {
          height: 39px;
          width: 14px;
          margin-left: 2px;
        }

        .title {
          margin: 30px auto 30px auto;
          text-align: center;
          font-weight: 500;
          font-size: 14px;
        }

        .login-register {
          text-align: center;
          margin-bottom: 10px;


          span {
            color: gray;
          }
        }
      }
    }
  }

  .logbox::before {

    content: "";
    position: absolute;
    top: -50%;
    left: -50%;
    width: 400px;
    height: 430px;
    transform-origin: bottom right;
    animation: animate 6s linear infinite;
    background: linear-gradient(0deg, transparent, rgb(96, 253, 206), rgb(255, 255, 255));
  }

  .logbox::after {

    content: "";
    position: absolute;
    top: -50%;
    left: -50%;
    width: 400px;
    height: 430px;
    transform-origin: bottom right;
    animation: animate 6s linear infinite;
    animation-delay: -3s;
    background: linear-gradient(0deg, transparent, rgb(94, 238, 195), rgb(255, 255, 255));
  }

  @keyframes animate {
    0% {
      transform: rotate(0deg);
    }

    100% {
      transform: rotate(360deg);
    }
  }


  .login-table {
    width: 400px;
    justify-content: center;
    display: flex;
    height: 40px;
    align-items: center;
    margin-bottom: 10px;

    img {
      height: 40px;
      width: 40px;
    }

    .title-log {
      color: white;
      opacity: 0.8;
      font-size: 20px;
      margin-left: 10px;
    }

  }

}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;
}

.login-code-img {
  height: 38px;
}

.animation-bg {
  position: absolute;
  top: 100vh;
  width: 100%;
  height: 100vh;
  display: flex;
  z-index: 999;

  .anbox1,
  .anbox2,
  .anbox3,
  .anbox4,
  .anbox5,
  .anbox6,
  .anbox7,
  .anbox8 {
    width: 250px;
    height: 100vh;
    background: linear-gradient(180deg, rgb(71, 186, 170), rgb(0, 163, 141), rgb(0, 151, 161));
    animation-fill-mode: forwards;
    animation-duration: 0.2s;
  }

  .anbox1 {
    animation-name: anil1;
  }

  @keyframes anil1 {
    to {
      transform: translateY(-100vh);

    }
  }

  .anbox2 {

    animation-name: anil2;
    animation-delay: 0.1s;

  }

  @keyframes anil2 {
    to {
      transform: translateY(-100vh);

    }
  }

  .anbox3 {
    animation-name: anil3;
    animation-delay: 0.2s;
  }

  @keyframes anil3 {
    to {
      transform: translateY(-100vh);

    }
  }

  .anbox4 {
    animation-name: anil4;
    animation-delay: 0.3s;
  }

  @keyframes anil4 {
    to {
      transform: translateY(-100vh);

    }
  }

  .anbox5 {
    animation-name: anil5;
    animation-delay: 0.4s;
  }

  @keyframes anil5 {
    to {
      transform: translateY(-100vh);

    }
  }

  .anbox6 {
    animation-name: anil6;
    animation-delay: 0.5s;
  }

  @keyframes anil6 {
    to {
      transform: translateY(-100vh);

    }
  }

  .anbox7 {
    animation-name: anil7;
    animation-delay: 0.6s;
  }

  @keyframes anil7 {
    to {
      transform: translateY(-100vh);

    }
  }

  .anbox8 {
    animation-name: anil8;
    animation-delay: 0.7s;
  }

  @keyframes anil8 {
    to {
      transform: translateY(-100vh);

    }
  }

}
</style>
